package com.klangner.go.ab.moveselector;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import com.klangner.go.ab.IMoveSelector;
import com.klangner.go.model.IBoard;
import com.klangner.go.model.IRuleChecker;
import com.klangner.go.model.Move;
import com.klangner.go.model.utils.BoardUtils;


/**
 * Selects all valid moves
 * 
 * @author Krzysztof Langner
 * @version 1.0
 */
public class AllMoveSelector implements IMoveSelector{

	private IRuleChecker rules;
	
	/**
	 * Constructor
	 * @param rules
	 */
	public AllMoveSelector(IRuleChecker rules){
		
		this.rules = rules;
	}
	
  //----------------------------------------------------------------------------
  /**
   * Finds all legal moves on the board
   * @see IMoveSelector#getCandidates(IBoard, int)
   */
  public Collection<Move> getCandidates( IBoard board, int color ) {

    Set<Move> moves = new HashSet<Move>();

    // Find all empty spaces
    for(int x = 0; x < board.getSize(); x++){
      for(int y = 0; y < board.getSize(); y++){
      	if( board.getStone(x, y) == IBoard.NO_STONE ){
      		Move move = new Move(x, y, color);
      		if(BoardUtils.hasLiberty(board, x, y)){
      			moves.add(move);
      		}
      		else{
      			// Check move according to rules
      			if(rules.isLegalMove(board, move)){
        			moves.add(move);
      			}
      		}
      	}
      }
    }
    
    return moves;
  }

}